/**
*  \file gpio.c
*
*  \brief
*
*  \author Dajin Li  <dajin.li@linde-.de>
*
*
*/

/*===============================================[ private includes  ]================================================*/
#include "../gpio/gpio.h"
#include "global.h"
#include "stm32f10x_gpio.h"
#include "stm32f10x_rcc.h"
#include "icons.h"

/*===============================================[ private defines   ]================================================*/

/*===============================================[ private datatypes ]================================================*/

/*===============================================[ private variables ]================================================*/

/*===============================================[ private functions ]================================================*/

/*================================================[ inline functions ]================================================*/

/*================================================[ public functions ]================================================*/


void GPIO_Initial(void)
{
	/*Forbed the PB3 JTDO Function*/
	/*Warning : Below two lines code can not modify, otherwise the key4 can not work normally*/
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
    GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE);
	
    GPIO_InitTypeDef  GPIO_InitStructure;
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);    //Enable PORTA RCC                                                               
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);    //Enable PORTB RCC 
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);    //Enable PORTC RCC 
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE);    //Enable PORTC RCC 
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);     //Enable CAN1 RCC 
	RCC_APB1PeriphClockCmd(	RCC_APB1Periph_SPI2,  ENABLE );  //Enable SPI RCC 
	/*PB5:LCD_RESET*/
	/*PB7:LCD_CS1*/
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_7;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOB, &GPIO_InitStructure);

	/*PA0:LCD_DB0*/
	/*PA1:LCD_DB1*/
	/*PA2:LCD_DB2*/
	/*PA3:LCD_DB3*/
	/*PA4:LCD_DB4*/
	/*PA5:LCD_DB5*/
	/*PA6:LCD_DB6*/
	/*PA7:LCD_DB7*/
	/*PA8:STCP595 --- PIN12 --- LATCH-CLOCK*/
    /*PA9:Buzzer*/
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOA, &GPIO_InitStructure);	

    /*PC0:LCD_CS2*/
	/*PC1:LCD_DI*/
	/*PC2:LCD_RW*/
	/*PC3:LCD_E*/
	/*PC7:DS595   --- PIN14 --- SERIAL-DATA_INPUT-A*/
	/*PC8:OE595   --- PIN13 --- OUTPUT-ENABLE*/
	/*PC9:SHCP595 --- PIN11 --- SHEFT-CLOCK*/
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9;;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOC, &GPIO_InitStructure);
    
/*************************************************Warning************************************************/
//NOTE!!!: When the PA10 Pin as key switch input detect pin, it must be configed into floating input mode,
//beceuse of the hardware circuit. If not, it may cause unpredictable errors .
/********************************************************************************************************/
	//Key Switch
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//GPIO_Mode_IPU;
    GPIO_Init(GPIOA, &GPIO_InitStructure);
	
	//Key1 ---- PB3
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
    GPIO_Init(GPIOB, &GPIO_InitStructure);
	
	//Key4 ---- PC11
	//Key3 ---- PC12
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11 | GPIO_Pin_12;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
    GPIO_Init(GPIOC, &GPIO_InitStructure);
	
	//Key2 ---- PD2
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
    GPIO_Init(GPIOD, &GPIO_InitStructure);
	
	PWR_vPVDInit();
    return;
} 

void PWR_vPVDInit(void)
{ 
    /* Configure the PVD Level to 3 and generate an interrupt on rising and falling
    edges(PVD detection level set to 2.9V, refer to the electrical characteristics
    of you device datasheet for more details) */
    PWR_PVDLevelConfig(PWR_PVDLevel_2V9);
    PWR_PVDCmd(ENABLE);
    while((PWR->CSR & PWR_CSR_PVDO)!=0);
}

void Buzzer_ON(void)
{
    GPIO_SetBits(BUZZER_PORT, BUZZER_PIN);
}
void Buzzer_OFF(void)
{
    GPIO_ResetBits(BUZZER_PORT, BUZZER_PIN);
}
/*===============================================[ private functions ]================================================*/


/***********************************************************************************************************************
*                             Copyright 2016 Linde Material Handling. All rights reserved.                             *
***********************************************************************************************************************/
